﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fireball.Extensions;
using System.Xml.Linq;
using System.Windows.Markup;

namespace Fireball.OpenXML.Word
{
    /// <summary>
    /// The Paragraph Implementation
    /// </summary>
    [ContentProperty("Inlines")]
    public class Paragraph : Block, IFindable, IAddChild
    {
        private InlineCollection _Inlines = null;

        public Paragraph()
        {
            _Inlines = new InlineCollection(this);
        }

        /// <summary>
        /// The collection of inline items for 
        /// this paragraph
        /// </summary>
        public InlineCollection Inlines
        {
            get
            {
                return _Inlines;
            }
        }

       /* public int LeftMargin
        {
            get;
            set;
        }

        public int RightMargin
        {
            get;
            set;
        }*/

        /// <summary>
        /// Contains the properties for this paragraph
        /// this property can be null
        /// </summary>
        public ParagraphProperties Properties
        {
            get;
            set;
        }        

        public override System.Xml.Linq.XContainer ToXml()
        {
            XElement p = new XElement(NS.WM + "p");


          /*  p.Add(new XElement(NS.WM + "pPr",
                new XElement(NS.WM + "ind",
                    new XAttribute(NS.WM + "left", this.LeftMargin *100),
                    new XAttribute(NS.WM + "right", this.RightMargin * 100)),
                new XElement(NS.WM + "jc",
                    new XAttribute(NS.WM + "val", this.Alignment.ToString().ToLower()))));
            */

            if (this.Properties != null)
                p.Add(this.Properties.ToXml());

            /*if (this.IsList)
            {
                p.Element(NS.WM + "pPr").AddFirst(
                    new XElement(NS.WM + "numPr",
                        new XElement(NS.WM + "ilvl",
                            new XAttribute(NS.WM + "val", 0)),
                        new XElement(NS.WM + "numId",
                            new XAttribute(NS.WM + "val", 1))));
            }*/

            /*if (!string.IsNullOrEmpty(this.Style))
            {
                p.Element(NS.WM + "pPr").Add(
                    new XElement(NS.WM + "pStyle",
                        new XAttribute(NS.WM + "val", this.Style)));
            }
            */
            foreach (Inline c in this.Inlines)
            {
                p.Add(c.ToXml());
            }
            return p;
        }

        public Paragraph(params Inline[] inlines)
        {
            _Inlines = new InlineCollection(this);
            this.Inlines.AddRange(inlines);
        }

        #region IFindable Members

        public OpenXMLEntity FindByName(string name)
        {
            foreach (var inline in this.Inlines)
            {
                if (inline.Name == name)
                    return inline;

                if (inline is IFindable)
                {
                    var s = ((IFindable)inline).FindByName(name);

                    if (s != null)
                        return s;
                }
            }

            return null;
        }

        #endregion

        #region IAddChild Members

        public void AddChild(object value)
        {
            if (!(value is Inline))
               throw new Exception("Invalid child element type of Paragraph");

            this.Inlines.Add((Inline)value);
        }

        public void AddText(string text)
        {
            this.Inlines.Add(new Run(text));
        }

        #endregion
    }
}
